﻿/**
 * Created by li on 2017/1/28.
 */
/**
 *
 * @param x location
 * @param y
 * @param type,(L|F)|(Z||N)|T|O|I,O is square
 *
 */
function FourGrid(type,x,y,direction){
    //x,y代表四格的左下角（1,1是四格的旋转中心，3,0是它的左下角）  在map中的位置
    this.x = x;
    this.y = y;

    //左下角是个固定值
    this.yy = 3;
    this.xx = 0;

    //值为1的临界值，是个数组
    // this.minX=[{0,0}],
    // this.maxX=[],
    this.minY=[{y:0,x:0}],

    this.type=type;
    this.fourGridArr=[];
    // this.isFalling = 1;//四格下落的状态，0，代表未初始化，1，生成后正在下落，2，落到下方不动，
    switch(type){
        case 'O':
            this.fourGridArr=[
                [0,0,0,0],
                [0,1,1,0],
                [0,1,1,0],
                [0,0,0,0]
            ];
            this.minY=[{y:2,x:1},{y:2,x:2}]
            break;
        case 'Z':
            this.fourGridArr=[
                [0,0,0,0],
                [1,1,0,0],
                [0,1,1,0],
                [0,0,0,0]
            ];
            this.minY=[{y:2,x:1},{y:2,x:2}]
            break;
        case 'N':
            this.fourGridArr=[
                [0,0,0,0],
                [0,1,1,0],
                [1,1,0,0],
                [0,0,0,0]
            ];
            break;
        case 'L':
            this.fourGridArr=[
                [0,1,0,0],
                [0,1,0,0],
                [0,1,1,0],
                [0,0,0,0]
            ];
            break;
        case 'F':
            this.fourGridArr=[
                [0,1,1,0],
                [0,1,0,0],
                [0,1,0,0],
                [0,0,0,0]
            ];
            break;
        case 'T':
            this.fourGridArr=[
                [1,1,1,0],
                [0,1,0,0],
                [0,0,0,0],
                [0,0,0,0]
            ];
            break;
        case 'I':
            this.fourGridArr=[
                [1,0,0,0],
                [1,0,0,0],
                [1,0,0,0],
                [1,0,0,0]
            ];
            break;
    }
    switch(direction){
        case 'up':
            break;
        case 'left':
            this.transform(this.fourGridArr);
            break;
        case 'left':
            this.transform(this.fourGridArr);
            this.transform(this.fourGridArr);
            break;
        case 'left':
            this.transform(this.fourGridArr);
            this.transform(this.fourGridArr);
            this.transform(this.fourGridArr);
            break;
    }
}
FourGrid.prototype = {
    constructor:FourGrid,
    //moveLeft:function(){
    //     this.x = this.x-1;
    //},
    //moveRight:function(){
    //    this.x = this.x+1;
    //},
    /**
     * 旋转90，逆时针，
     * 未解决旋转后创墙问题,碰撞检测问题
     *
     */
    transform:function(){
        //正方形不用旋转
        if(this.fourGridArr[1][1]==1&&this.fourGridArr[1][2]==1&&this.fourGridArr[2][1]==1&&this.fourGridArr[2][2]==1){
            //alert('a');
            return this.fourGridArr;
        }
        //将4*4数组转换3*3数组，这样3*3的中心旋转相当于4*4的（1,1）处旋转
        var temp33 = [];
        for(var i=0;i<3;i++){
            temp33[i] = [];
            for(var j=0;j<3;j++){
                temp33[i][j] = 0;
            }
        }

        //数组逆时针90,
        for(var i=0;i<3;i++){
            for(var j=0;j<3;j++){
                temp33[2-j][i] = this.fourGridArr[i][j];
                //temp33[j][2-i] = array[i][j];//顺时针90
            }
        }
        //console.log(temp33);
        //3*3扩展4*4
        temp33[3] = [0,0,0,0];
        for(var i=0;i<4;i++){
            for(var j=3;j<4;j++){
                temp33[i][j] = 0;
            }
        }
        //直线旋转特殊对待
        //竖线旋转
        if(this.fourGridArr[3][0]==1) {
            temp33 = [
                [1,1,1,1],
                [0,0,0,0],
                [0,0,0,0],
                [0,0,0,0]
            ];
            //横线旋转
        }else if(this.fourGridArr[0][3]==1) {
            temp33 =[
                [1,0,0,0],
                [1,0,0,0],
                [1,0,0,0],
                [1,0,0,0]
            ]
        }
        this.fourGridArr = temp33;
        //return temp33;
    },
    getType : function () {
        return this.type;
    },

}


